<!DOCTYPE html>
<html lang="en">
<script src="https://cdn.jsdelivr.net/npm/mammoth@1.4.8/mammoth.browser.min.js"></script>

<body>
  <input type="file" id="fileInput">
  <script>
    document.getElementById('fileInput').addEventListener('change', function (event) {
      const files = event.target.files; // 获取文件列表
      // 你可以在这里处理文件，例如读取文件内容或获取文件信息
      var file = files[0];
      const reader = new FileReader()
      reader.readAsArrayBuffer(file)
      reader.onload = function (evt) {
        const arrayBuffer = evt.target.result
        mammoth.extractRawText({ arrayBuffer: arrayBuffer }).then((res) => {
          doc4(res)
          // doc3(res)
          // doc2(res)
          // doc1(res)
        })
      }
    });
    function delFn(all,i){
      all[i] = all[i]+all[i+1]
      all.splice(i+1,1)
    }
    function doc1(res) {
      let allArr = res.value.replaceAll('  ', '').replaceAll('　　', '').replaceAll(/\d+./g, '').split('\n\n').slice(1)

      let resArr = []
      allArr.forEach(t => {
        // 去除首尾空格的单词串
        let noneSpaceStr = t.replace(/^\s+|\s+$/g, "")
        if (noneSpaceStr) {
          let wordArr = noneSpaceStr.split(' ')
          let meaning = wordArr.slice(1).join(' ')
          if (meaning.length > 3) {
            resArr.push({
              word: wordArr[0],
              meaning
            })
          }
        }
      })
      console.log(JSON.stringify(resArr))
      return resArr
    }
    function doc2(res) {
      const data1 = res.value.split('\n\n').filter(t => Boolean(t))
      // 前面的单词
      const headData = data1.splice(0, 3368)
      let headArr = []
      for(let i = 0;i < headData.length;i++){
        let theNumType = (headData[i].substr(0,1)*1)
        if(typeof theNumType === 'number' && !isNaN(theNumType)){
          let word = headData[i].split('.')[1]
          let meaning = headData[i+1]
          headArr.push({
            word,
            meaning
          })
        }
      }
      console.log(JSON.stringify(headArr))
      //后面的短语
      const tailData = data1.filter(t => t.indexOf('组') === -1).slice(1)
      let tailArr = []
      for (let i = 0; i < tailData.length; i += 3) {
        const index = tailData[i];     // 单词的序号
        const word = tailData[i + 1];  // 单词本身
        const meaning = tailData[i + 2]; // 单词释义

        // 将结果存储为对象，并添加到结果数组中
        tailArr.push({ word: word, meaning: meaning });
      }
      console.log(JSON.stringify(tailArr))
    }
    function doc3(res){
      let all = res.value.split('\n').filter(t => {
        if(Boolean(t) && t !== '?' && t.indexOf('Unit') === -1 && t.indexOf('http') === -1 && isNaN(t.substr(0,1)*1)){
          return true
        }
      }).slice(1)
      all[1639] = all[1639]+all[1640]
      all.splice(1640,1)
      all[4333] = all[4333]+all[4334]
      all.splice(4334,1)
      let headArr = []
      for(let i = 0;i < all.length;i+=2){
        let word = all[i]
          let meaning = all[i+1]
          headArr.push({
            word,
            meaning
          })
      }
      console.log((headArr))
    }
    function doc4(res){
      let all = res.value.split('\n').filter(t => {
        if(Boolean(t) && t !== '?' && t.indexOf('Unit') === -1 && t.indexOf('http') === -1 && isNaN(t.substr(0,1)*1)){
          return true
        }
      }).slice(1)
      let arr = []
      for(let i = 0;i < all.length;i+=2){
        let word = all[i]
          let meaning = all[i+1]
          arr.push({
            word,
            meaning
          })
      }
      console.log(JSON.stringify(arr))
    }
  </script>
</body>

</html>